;(function (window) {

var pageLogic = {
    init: {
        before: function () {
            var columns = [
                {field: "checked", checkbox: true},
                {field: "num", title: "序号", width: 5, align: "center", formatter: common.formatter.index},
                {field: "id", visible: false},
                {field: "qlmc", title: "桥梁名称", width: 80, align: "center"},
                {field: "qlbm", title: "桥梁编码", width: 80, align: "center"},
                {field: "hdmc", title: "所属航道", width: 80, align: "center"},
                {field: "htmc", title: "所属航段", width: 80, align: "center"},
                {field: "simpleName", title: "管理单位", width: 80, align: "center"},
                {field: "lcz", title: "里程桩", width: 80, align: "center",formatter:pageLogic.formatter.lczformatter},
            ];

            common.initTable(columns);
        },

        layout: function() {
            common.layout();
        },
        after: function () {
            //创建模态窗口
            common.modal(pageLogic.initData.modalParams[0], {
            rules: {
                qlmc : {required:true,maxLen:50,},
                qlbm : {required:true,maxLen:20},
                gljgid : {required:true},
                hdpk : {required:true},
                htpk : {required:true},
                lczm : {required:true, maxLen:3,digits:true},
                lczk : {required:true,digits:true}
            },

            messages: {}
            });

            $("#gljgid").selectTree({
                url : masterpage.ctxp + "/sys/orgs/-1/tree"
            });

            $("#hdpk").select({
                url : masterpage.ctxp + "/basic/hds/getAllhd",
                textField : "hdmc",
                valueField : "id",
                responseKey : "data",
                headerKey : "请选择",
                headerValue : ""
            });

            $("#hdpkSearch").select({
                url : masterpage.ctxp + "/basic/hds/getAllhd",
                textField : "hdmc",
                valueField : "id",
                responseKey : "data",
                headerKey : "请选择",
                headerValue : ""
            });
        },
        //页面控件事件绑定(一般为按钮的事件绑定)
        events: function () {
            common.registerEvents();

            $("#hdpk").on("change", function() {
                var hdId = $("#hdpk").val() || "-1";
                $("#htpk").select({
                    url : masterpage.ctxp + "/basic/hts/" + hdId + "/getHtByHdId",
                    textField : "htmc",
                    valueField : "id",
                    responseKey : "data"
                });
            });

            $("#lczk").on("input propertychange", function() {
                var lczk = $("#lczk").val();
                var lczm = $("#lczm").val();
                var lcz = lczk * 1 + (lczm/1000).toFixed(3) * 1;
                $("#lcz").val(lcz.toFixed(3));
            });

            $("#lczm").on("input propertychange", function() {
                var lczk = $("#lczk").val();
                var lczm = $("#lczm").val();
                var lcz = lczk * 1 + (lczm/1000).toFixed(3) * 1;
                $("#lcz").val(lcz.toFixed(3));
            });

            // 注册修改按钮点击事件
            $("#modifyBtn").off("click").on("click", function () {
                var selects = pageLogic.btTable.bootstrapTable('getSelections');
                if (selects.length === 0) {
                    layer.msg("需要先选中一个");
                    return;
                }

                if (selects.length > 1) {
                    layer.msg("只能选中一个");
                    return;
                }

                common.update({
                    url: pageLogic.initData.restUrlPrefix + "/" + selects[0]["id"],
                    modal: pageLogic.modals[0],
                    idField: "id",
                    hook: function (data) {
                        //
                        if (data["lcz"] != "") {

                            var lcz = data["lcz"];

                            $("#lczk").val(Math.floor(lcz));

                            $("#lczm").val((lcz - Math.floor(lcz)).toFixed(3) * 1000);
                        }
                    }
                });

            });



        },

        load: function () {
            common.search();
        }
    }
};

pageLogic.formatter = {
    lczformatter : function (value, row) {
        var K = "";
        var M = "";

        K = Math.floor(value);
        M = (value - K).toFixed(3) * 1000

        return K + "K+" + M + "M";
    }
};

window.pageLogic = pageLogic;
})(window);
